package com.qqtech.qquan.draw.controller;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.data.domain.Sort.Direction;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.google.common.collect.Lists;
import com.qqtech.core.common.annotation.VisitAnnt;
import com.qqtech.core.common.enums.RespEnum;
import com.qqtech.core.common.enums.YesNoEnum;
import com.qqtech.core.common.util.StringUtil;
import com.qqtech.core.common.util.TimeUtil;
import com.qqtech.core.frame.model.PageKit;
import com.qqtech.core.frame.model.PageOrder;
import com.qqtech.core.frame.model.Result;
import com.qqtech.core.frame.service.BaseService;
import com.qqtech.qquan.circle.model.CircleBaseVo;
import com.qqtech.qquan.circle.service.CircleBaseService;
import com.qqtech.qquan.draw.enums.DrawBaseTypeEnum;
import com.qqtech.qquan.draw.enums.DrawRageTypeEnum;
import com.qqtech.qquan.draw.model.DrawBase;
import com.qqtech.qquan.draw.model.DrawBaseQuery;
import com.qqtech.qquan.draw.model.DrawBaseVo;
import com.qqtech.qquan.draw.model.DrawBatchQuery;
import com.qqtech.qquan.draw.model.DrawBatchVo;
import com.qqtech.qquan.draw.service.DrawBaseService;
import com.qqtech.qquan.draw.service.DrawBatchService;
import com.qqtech.qquan.draw.service.DrawItemService;
import com.qqtech.qquan.draw.service.DrawSysParamService;
import com.qqtech.qquan.user.model.UserDrawQuery;
import com.qqtech.qquan.user.model.UserDrawVo;
import com.qqtech.qquan.user.service.UserBaseService;
import com.qqtech.qquan.user.service.UserDrawService;
import com.qqtech.web.context.BaseWebController;

@Controller
@RequestMapping(value = "/draw/base")
public class DrawBaseController extends BaseWebController<DrawBase, DrawBaseQuery> {
	@Resource
	private DrawBaseService drawBaseService;

	@Resource
	private DrawItemService drawItemService;
	
	@Resource
	private DrawBatchService drawBatchService;

	@Resource
	private UserDrawService userDrawService;
	
	@Resource
	private UserBaseService userBaseService;

	@Resource
	private DrawSysParamService drawSysParamService;
	
	@Resource
	private CircleBaseService circleBaseService;
	
	@Override
	protected BaseService<DrawBase> getBaseService() {
		return drawBaseService;
	}

	/**
	 * detail
	 * 
	 * @param drawBaseQuery
	 * @return
	 * 如果是社区签到后打开-本社区抽奖,如果没有本社区抽奖就查询平台抽奖，传入communityId=xxx,type=1
	 * 如果是圈子签到后打开-平台抽奖，如果没有平台抽奖就查询本圈子所在社区的社区抽奖，传入circleId=yyy,type=2
	 * 如果是其他签到，直接打开平台抽奖，传入type=3
	 */
	@VisitAnnt(type = RespEnum.LOGIN)
	@RequestMapping(value = "/detail")
	@ResponseBody
	public Result detail(@RequestBody DrawBaseQuery drawBaseQuery) {
		Result result = new Result();
		DrawBaseVo drawBaseVo = new DrawBaseVo();
		drawBaseQuery.setNow(TimeUtil.now());
		drawBaseQuery.setStatus(YesNoEnum.YES.getCode());
		List<DrawBaseVo> list = Lists.newArrayList();
		
		if(drawBaseQuery.getType()==DrawBaseTypeEnum.COMMUNITY.getCode()){
			//如果是社区签到，先查询当前社区的抽奖，如果本社区没有抽奖，再查询平台抽奖
			list = drawBaseService.queryList(drawBaseQuery);
			if(list==null || list.size()==0){
				drawBaseQuery.setCommunityId(null);//去掉社区id属性，查询平台抽奖
				drawBaseQuery.setRangeType(DrawRageTypeEnum.PLATFORM.getCode());//平台抽奖
				list = drawBaseService.queryList(drawBaseQuery);
			}
		}else if(drawBaseQuery.getType()==DrawBaseTypeEnum.CIRCLE.getCode()){
			//如果是圈子签到，优先查询平台抽奖,如果没有，就查询本圈子所在社区的社区抽奖
			drawBaseQuery.setRangeType(DrawRageTypeEnum.PLATFORM.getCode());//平台抽奖
			list = drawBaseService.queryList(drawBaseQuery);
			if(list==null || list.size()==0){
				CircleBaseVo circle = circleBaseService.getById(drawBaseQuery.getCircleId());
				if(circle!=null){
					if(circle.getCommunityId()!=null){
						drawBaseQuery.setRangeType(DrawRageTypeEnum.COMMUNITY.getCode());//社区抽奖【圈子所在社区】
						drawBaseQuery.setCommunityId(circle.getCommunityId());
						list = drawBaseService.queryList(drawBaseQuery);
					}
				}
				
			}
		}else{
			drawBaseQuery.setRangeType(DrawRageTypeEnum.PLATFORM.getCode());//平台抽奖
			list = drawBaseService.queryList(drawBaseQuery);
		}
		
		if (null != list && list.size() > 0) {
			drawBaseVo = list.get(0);
			DrawBatchQuery drawBatchQuery = new DrawBatchQuery();
			drawBatchQuery.setDrawBaseId(drawBaseVo.getId());
			String imgHandleDomain = drawBatchQuery.getImgHandleDomain();
			List<DrawBatchVo> items = drawBatchService.queryList4Draw(drawBatchQuery, new PageKit(new PageOrder(Direction.ASC, "id")));
			for (DrawBatchVo drawBatchVo : items) {
				String drawItemImg = drawBatchVo.getDrawItemImg();
				if (StringUtil.isNotBlank(drawItemImg)) {
					drawBatchVo.setDrawItemImg(imgHandleDomain + drawItemImg);
				}
				/*
				drawItemVo=drawItemService.getById(drawBatchVo.getDrawItemId());
				drawBatchVo.setDrawItemName(drawItemVo.getName());
				drawBatchVo.setDrawItemImg(drawItemVo.getPathImgs());
				System.out.println("DrawBatchVo======================DrawItemName==="+drawBatchVo.getDrawItemName()+"------DrawItemImg==="+drawBatchVo.getDrawItemImg());
				*/
			}
			UserDrawQuery userDrawQuery = new UserDrawQuery();
			userDrawQuery.setDrawBaseId(drawBaseVo.getId());
			List<UserDrawVo> userDrawList = userDrawService.queryList4Draw(userDrawQuery, new PageKit(1,20,new PageOrder(Direction.DESC, "id")));
			/*
			for (UserDrawVo userDrawVo : userDrawList) {
				DrawItemVo drawItemVo2=drawItemService.getById(userDrawVo.getDrawItemId());
				userDrawVo.setDrawItemName(drawItemVo2.getName());
				UserBaseVo userBaseVo=userBaseService.getById(userDrawVo.getUserId());
				userDrawVo.setUserNick(userBaseVo.getNick());
				System.out.println("UserDrawVo======================DrawItemName==="+userDrawVo.getDrawItemName()+"------UserNick==="+userDrawVo.getUserNick());
			}
			*/
			result.put("userDrawList", userDrawList);
			result.put("items", items);
			result.put("drawBaseVo", drawBaseVo);
		}else{
			DrawBaseQuery drawBaseQuery2 = new DrawBaseQuery();
			drawBaseQuery2.setStatus(YesNoEnum.YES.getCode());
			List<DrawBaseVo> list2 = drawBaseService.queryList(drawBaseQuery2, new PageKit(new PageOrder(Direction.ASC, "id")));
			if(null!=list2&&list2.size()>0){
				if(TimeUtil.now().before(list2.get(0).getStartAt())){
					result.addErr("抽奖还未开始，敬请期待。");
				}
				if(TimeUtil.now().after(list2.get(0).getEndAt())){
					result.addErr("本轮抽奖已结束，请期待下一轮。");
				}
			}else{
				result.addErr("");
			}
		}
		return result;
	}

}
